package me.zhengjie.domain;

import lombok.RequiredArgsConstructor;
import me.zhengjie.config.RegisterAddressConfig;
import me.zhengjie.dto.sqlServer.ProductionInfo;
import me.zhengjie.repository.ProductionInfoRepository;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Component
@RequiredArgsConstructor
public class ProInfoService {

    private final ProductionInfoRepository productionInfoRepository;

    @Transactional
    public void startUnbinding(String userName, Timestamp startTime, String palletCode ,Integer id, String stationName){
        productionInfoRepository.startUnbindingProduction(userName,startTime,palletCode, id, stationName);
    }

    public ProductionInfo getNewFinishLayUpPallet(String palletCode){
        List<ProductionInfo> productionInfos = productionInfoRepository.findProductionInfosOrderByLayUpFinishTime(palletCode);
        return productionInfos.get(0);
    }

    @Transactional
    public void finishUnbinding(Timestamp finishTime, String palletCode, Integer id){
        productionInfoRepository.finishUnbindingProduction(finishTime, palletCode ,id);
    }

    @Transactional
    public void startPressing(Timestamp startTime, String palletCode){
        ProductionInfo productionInfo = getNewFinishLayUpPallet(palletCode);
        Integer id = productionInfo.getId();
        productionInfoRepository.startPressing(startTime, id);
    }

    @Transactional
    public void finishPressing(Timestamp finishTime, String palletCode){
        productionInfoRepository.finishPressing(finishTime, palletCode);
    }

    public ProductionInfo getUnbindingProductionInfo(String unbindingStation){
        List<ProductionInfo> productionInfos = productionInfoRepository.findProductionInfoByStatus(5,unbindingStation);
        return productionInfos.get(0);
    }

    public ProductionInfo getNewUnbindingProductionInfo(String palletCode){
        return productionInfoRepository.findNewProductionInfo(palletCode).get(0);
    }
}
